home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / OSAX Samples / Four Password OSAXen / PassWord2 Folder / PassWord2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  4.0 KB  |  154 lines  |  [TEXT/KAHL]

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Password2.c
  4. //    Written by: Donald Olson
  5. //    May 1993
  6. //    Copyright ® 1993 Apple Computer Inc.
  7. //     All rights reserved.
  8. //
  9. //    This is a simple Scripting Addition that asks the user to type in a
  10. //    password.  This is different from PassWord.c in that the users 
  11. //     password is stored in the refCon field so that it only needs to be 
  12. //    entered once. This was written for a talk at the 1993 WWDC given
  13. //     by Donald Olson and Donn Denman.
  14. //
  15. //    Syntax: User Password2
  16. //    Return: Encrypted form of user password or cancel.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include <string.h>
  21. #include <Dialogs.h>
  22. #include <AppleEvents.h>
  23. #include <Memory.h>
  24.  
  25. // Our headers
  26. void        FrameDefault( DialogPtr theDialog, long theItem);
  27. pascal     Boolean MyDlgFilter(    DialogPtr theDlg, 
  28.                             EventRecord *theEventRec, 
  29.                             short *itemHit);
  30. void     EncryptString(StringPtr strPtr);
  31.  
  32. // Our constants
  33. #define DLOGID     128
  34. #define MYClass     'OLIE'
  35. #define MyID        'psw2'
  36.  
  37. pascal OSErr main(AppleEvent *theEvent,
  38.                 AppleEvent *theReply, 
  39.                 long theRefCon)
  40. {
  41.     DialogPtr             theDialog = nil;
  42.     OSErr                theErr = noErr;
  43.     GrafPtr                savedPort;
  44.     short                itemHit;
  45.     Str32                theString;
  46.     StringPtr                theStrPtr = (StringPtr)&theString;
  47.     StringHandle            theStrHdl = (StringHandle)&theStrPtr;
  48.     EventHandlerProcPtr    handler;
  49.     long                    handlerRefcon;
  50.     Handle                ourGlobalHdl = nil;
  51.     
  52.     if(theRefCon != 0) // We've got a stored password.
  53.         goto ADDPARAM;
  54.         
  55.     GetPort(&savedPort);
  56.     
  57.     theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  58.     if(theErr != noErr) return theErr;
  59.     
  60.     theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);    
  61.         
  62.         if(theDialog == nil) return resNotFound;        
  63.        
  64.         SetPort(theDialog);
  65.     FrameDefault(theDialog, ok);
  66.     
  67.     strcpy((char*)theString, (char*) "\0"); // Empty string
  68.     SetWRefCon(theDialog, (long)theStrHdl);
  69.     
  70.     do {
  71.         ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
  72.     } while (itemHit != cancel && itemHit != ok);
  73.      if(itemHit == cancel) {
  74.          DisposDialog(theDialog);
  75.          return userCanceledErr;
  76.      }
  77.     
  78.     DisposDialog(theDialog);
  79.     SetPort(savedPort);
  80.     
  81.     EncryptString(theStrPtr);
  82.     
  83.     theErr = AEGetEventHandler(MYClass, MyID, 
  84.                             &handler, &handlerRefcon, true);
  85.     
  86.     /* Make us some global storage */
  87.     ourGlobalHdl = NewHandleSysClear(sizeof(theString));
  88.     if(ourGlobalHdl == nil)
  89.         return MemError();
  90.     
  91.     SetString(ourGlobalHdl, theString);    // Copy string to our handle
  92.         
  93.     theErr = AEInstallEventHandler(MYClass, MyID, 
  94.                                 handler, (long)ourGlobalHdl, true);
  95.     
  96.     theRefCon = (long)ourGlobalHdl; // For our local use.
  97.     
  98.     ADDPARAM:;
  99.     HLock((Handle)theRefCon);
  100.     theErr = AEPutParamPtr(    theReply, keyDirectObject, typeChar, 
  101.                             (Ptr)*(Handle)theRefCon, 
  102.                             strlen((char*)*(Handle)theRefCon));
  103.     
  104.     HUnlock((Handle)theRefCon);
  105.     return theErr;
  106. }
  107.  
  108. void EncryptString(StringPtr strPtr) {
  109.     short    theSize, x;
  110.     theSize = strlen((char*)(strPtr));
  111.     
  112.     for( x = 0; x <= (theSize -1); x++) {
  113.         strPtr[x] = (short)strPtr[x] + (short)"\¡";
  114.     }
  115. }
  116.  
  117. pascal Boolean MyDlgFilter(DialogPtr theDlg, EventRecord *theEventRec, short *itemHit) {    
  118.     
  119.     StringHandle    theStrHdl;
  120.     short            theSize;
  121.     if(theEventRec->what != keyDown) // Just looking for keystrokes
  122.         return false;
  123.             
  124.     switch((theEventRec->message) & charCodeMask)
  125.     {
  126.         case 0x0d:                // Return
  127.             *itemHit = ok;      
  128.             return true;
  129.         case 0x03:                // Enter
  130.             *itemHit = ok;      
  131.             return true;
  132.         case 0x08:                // Backspace
  133.             return false;
  134.         default:
  135.             theStrHdl = (StringHandle)GetWRefCon(theDlg);
  136.             theSize = strlen((char*)(*theStrHdl));
  137.             (*theStrHdl)[theSize] = (char)(theEventRec->message);
  138.             (*theStrHdl)[theSize + 1] = (char)NULL;
  139.             theEventRec->message = '•';
  140.             return false;
  141.     }
  142. }
  143.  
  144. void FrameDefault( DialogPtr theDialog, long theItem) {
  145.     short    DType; 
  146.     Handle    DItem;
  147.     Rect    DRect;
  148.     
  149.     GetDItem(theDialog, theItem, &DType, &DItem, &DRect); 
  150.     PenSize(3, 3); 
  151.     InsetRect(&DRect, -4, -4);
  152.     FrameRoundRect(&DRect, 16, 16);
  153.     PenSize(1, 1);
  154. }